package com.example.demo.module.repository.controller;

import com.example.demo.common.entity.enums.RepositoryTypeEnum;
import com.example.demo.common.entity.response.ResCode;
import com.example.demo.common.entity.response.Rs;
import com.example.demo.constant.Constant;
import com.example.demo.entity.request.repository.RepositoryRequest;
import com.example.demo.module.repository.service.RepositoryService;
import com.example.demo.utils.CommonUtils;
import com.example.demo.utils.EnumUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 仓库控制器.
 *
 * @author Mei Ruoxiao
 * @since 2020/9/11
 */
@Slf4j
@RestController("repositoryc")
@RequestMapping("/repository")
@Api(tags = "仓库接口")
public class RepositoryController {

    @Resource
    private RepositoryService service;

    /**
     * 新增仓库文件夹
     *
     * @param request   仓库请求对象
     * @param companyId 企业ID
     * @author Mei Ruoxiao
     * @since 2020/9/11
     */
    @PostMapping
    @ApiOperation(value = "新增文件夹")
    public ResponseEntity<Rs<Object>> create(
            @Valid @RequestBody RepositoryRequest request,
            @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) long companyId) {
        log.info("create:创建文件夹[request:{},companyId:{}]", request, companyId);
        // 基础验证
        if (Objects.isNull(EnumUtil.of(RepositoryTypeEnum.class, request.getType()))) {
            return Rs.fail(ResCode.COMPANY_PARAM_TYPE_ERROR);
        }
        if (!StringUtils.hasText(request.getName())) {
            return Rs.fail(ResCode.REPOSITORY_NAME_NOT_EMPTY);
        }
        if (request.getName().length() > Constant.NAME_LENGTH_FIFTY) {
            return Rs.fail(ResCode.REPOSITORY_NAME_LENGTH_NOT_EXIST);
        }
        return Rs.ok(CommonUtils.getIdResponse(service.create(request, companyId)));
    }

    /**
     * 修改文件夹名称
     *
     * @param id      文件夹ID
     * @param request 仓库请求对象
     * @author Mei Ruoxiao
     * @since 2020/9/15
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "修改文件夹名称")
    public ResponseEntity<Rs<Object>> update(
            @ApiParam(value = "id", example = "852842544111") @PathVariable("id") Long id,
            @Valid @RequestBody RepositoryRequest request,
            @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) long companyId) {
        log.info("update:修改文件夹名称[id:{}, request:{}, companyId:{}]", id, request, companyId);
        if (CommonUtils.isErrorId(id)) {
            return Rs.wrongFormat("id");
        }
        return Rs.ok(service.update(id, request, companyId));
    }


    /**
     * 删除文件夹
     *
     * @param id 文件夹ID
     * @author Mei Ruoxiao
     * @since 2020/9/12
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除文件夹")
    public ResponseEntity<Rs<Object>> delete(
            @ApiParam(value = "id", example = "18246537921") @PathVariable("id") Long id,
            @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) long companyId) {
        log.info("create:删除文件夹[id:{},companyId:{}]", id, companyId);
        if (CommonUtils.isErrorId(id)) {
            return Rs.wrongFormat("id");
        }
        return Rs.ok(service.delete(id, companyId));
    }


    /**
     * 获取文件夹树
     *
     * @author Mei Ruoxiao
     * @since 2019/9/14
     */
    @GetMapping(value = "/tree")
    @ApiOperation(value = "获取文件夹树")
    public ResponseEntity<Rs<Object>> tree(
            @ApiIgnore @RequestParam(value = Constant.HeaderKey.COMPANY_ID, required = false) long companyId,
            @ApiParam(value = "type", example = "1") @RequestParam("type") int type) {
        log.info("tree:获取文件夹树[companyId:{},type:{}]", companyId, type);
        if (Objects.isNull(EnumUtil.of(RepositoryTypeEnum.class, type))) {
            return Rs.fail(ResCode.COMPANY_PARAM_TYPE_ERROR);
        }
        return Rs.ok(service.tree(type, companyId));
    }
}
